package com.saic.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class HbaseTest {
	public static InputStream in = null;
	public static Configuration conf = null;
	private static Connection con =  null;
	private static TableName tableName = TableName.valueOf("test00");
	private static Table webTable =null;
	static {
		try{
		in = HbaseTest.class.getClassLoader().getResourceAsStream("hbaseconf.properties");
		Properties properties = new Properties();
		properties.load(in);
		conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", properties.getProperty("hbase_zookeeper_quorum"));
		conf.set("hbase.zookeeper.property.clientPort", properties.getProperty("hbase_zookeeper_property_clientPort"));
		con = ConnectionFactory.createConnection(conf);
		
		webTable = con.getTable(tableName);
		}catch(Exception e){
			
		}
	}
	
	
	
	
	public static void main(String[] args) throws IOException {
		new HbaseTest();
		
		TableName tableName = TableName.valueOf("test00");
		Table table = con.getTable(tableName);
		
		Get get=new Get("146846147714151".getBytes());
		Result result=table.get(get);
		for(KeyValue key:result.list()){
			System.out.println(Bytes.toString(key.getRow()));
			
		}
//		Admin admin = (HBaseAdmin) con.getAdmin();
		
	}
	/**
	 * put data interface
	 * @param map
	 */
	public static void putData(Map<String,String> map){
	}
	public static boolean putWebData(Map<String,Object> webMap){
		if( null ==webMap){
			return false;
		}
		Put put = new Put(Bytes.toBytes(webMap.get("__timestamp__").toString()));
		for(String key: webMap.keySet()){
		  put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes(key), Bytes.toBytes(webMap.get(key).toString()));
		}
		try {
			
			webTable.put(put);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}
	 public static void createHbaseCountTable(String HbaseCountTableName) throws Exception{
	    	List<String> familyList = new ArrayList<String>();
			familyList.add("other");
			familyList.add("cf");
			HBaseAdmin hAdmin = new HBaseAdmin(conf);
			if (hAdmin.tableExists(HbaseCountTableName)) {
				System.out.println("表 " + HbaseCountTableName + " 已存在！");
			} else {
				// 新建一个students表的描述
				HTableDescriptor tableDesc = new HTableDescriptor(HbaseCountTableName);
				// 在描述里添加列族
				for (String columnFamily : familyList) {
					tableDesc.addFamily(new HColumnDescriptor(columnFamily));
				}
				// 根据配置好的描述建表
				hAdmin.createTable(tableDesc);
				System.out.println("创建表 " + HbaseCountTableName + " 成功!");
			}
	    }
}
